package com.iot.ammeter.view.fragments

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.material3.ExperimentalMaterial3Api
import com.iot.ammeter.IApplication
import com.iot.ammeter.common.rmbString
import com.iot.ammeter.databinding.FragmentUserCenterBinding
import com.iot.ammeter.view.AuthActivity
import com.iot.ammeter.view.BalanceFillActivity
import com.iot.ammeter.view.ElectricBillActivity
import com.iot.ammeter.view.PaymentRecordsActivity

class UserCenterFragment : Fragment() {

    private lateinit var _binding: FragmentUserCenterBinding
    private val binding: FragmentUserCenterBinding get() = _binding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        // Inflate the layout for this fragment
        return FragmentUserCenterBinding.inflate(inflater, container, false).also {
            _binding = it
        }.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.apply {
            balanceCard.content = rmbString(IApplication.userInfo!!.balance)
            balanceCard.setOnClickListener {
                BalanceFillActivity.launchActivity(requireContext())
            }
            paymentRecordCard.setOnClickListener {
                PaymentRecordsActivity.launchActivity(requireContext())
            }
            electricityBillCard.setOnClickListener {
                ElectricBillActivity.launchActivity(requireContext())
            }
            logoutButton.setOnClickListener {
                IApplication.apply {
                    token = null
                    userInfo = null
                    deviceInfo = null
                    lastMonthInfo = null
                    startActivity(Intent(requireContext(), AuthActivity::class.java))
                    requireActivity().finish()
                }
            }
        }
    }
}